<?php

session_start();
include_once dirname(dirname(__FILE__)) . '/include/core/image.php';
include_once dirname(dirname(__FILE__)) . '/include/common.php';
$core = new core();
$product_id = isset($_POST["product_id"]) ? intval($_POST["product_id"]) : 0;
$product_name = isset($_POST["product_name"]) ? addslashes(trim($_POST["product_name"])) : '';
$product_code = isset($_POST["product_code"]) ? addslashes(trim($_POST["product_code"])) : '';
$link_web = isset($_POST["link_web"]) ? addslashes(trim($_POST["link_web"])) : '';
$product_price = isset($_POST["product_price"]) ? $_POST["product_price"] : 0;
$store_price = isset($_POST["store_price"]) ? $_POST["store_price"] : 0;
$stamp_price = isset($_POST["stamp_price"]) ? $_POST["stamp_price"] : 0;
$product_size = isset($_POST["product_size"]) ? addslashes(trim($_POST["product_size"])) : '';
$product_color = isset($_POST["product_color"]) ? addslashes(trim($_POST["product_color"])) : '';
$manufacture = isset($_POST["manufacture"]) ? addslashes(trim($_POST["manufacture"])) : '';
$product_category = isset($_POST["product_category"]) ? intval($_POST["product_category"]) : '';
$folder_temp = isset($_POST["folder_temp"]) ? addslashes(trim($_POST["folder_temp"])) : '';
$file_main_photo_temp = isset($_POST["file_main_photo_temp"]) ? addslashes(trim($_POST["file_main_photo_temp"])) : '';
$array_extra_photo_name = isset($_POST["array_extra_photo_name"]) ? $_POST["array_extra_photo_name"] : array();
$description = isset($_POST["description"]) ? addslashes(trim($_POST["description"])) : '';
$extra_photo_list = isset($_SESSION["photo_name_lists"]) ? $_SESSION["photo_name_lists"] : array();
$extra_color_list = isset($_SESSION["photo_color_lists"]) ? $_SESSION["photo_color_lists"] : array();
$from_order_detail = isset($_POST["from_order_detail"]) ? $_POST["from_order_detail"] : 0;
//edit price
$product_price = floatval(preg_replace('/[^\d.]/', '', $product_price));
$product_price = $product_price * 1000;
$store_price = floatval(preg_replace('/[^\d.]/', '', $store_price));
$stamp_price = floatval(preg_replace('/[^\d.]/', '', $stamp_price));

if ($product_id > 0) {
    $product = $core->getProductById($product_id);
    if (!$product) {
        echo json_encode(array('type' => 1, 'msg' => "Sản phẩm không tồn tại!"));
        die;
    }
} else {
    $check_exist = $core->getProductInfoByCode($product_code);
    if ($check_exist) {
        echo json_encode(array('type' => 1, 'msg' => "Sản phẩm đã tồn tại, vui lòng tìm và sửa sản phẩm này!"));
        die;
    }
}
if ($product_name == "") {
    echo json_encode(array('type' => 1, 'msg' => "Vui lòng điền tên sản phẩm!"));
    die;
}
if ($product_code == "") {
    echo json_encode(array('type' => 1, 'msg' => "Vui lòng điền mã sản phẩm!"));
    die;
}
if ($manufacture == "") {
    echo json_encode(array('type' => 1, 'msg' => "Vui lòng điền thương hiệu!"));
    die;
}
if ($product_category == "") {
    echo json_encode(array('type' => 1, 'msg' => "Vui lòng nhập loại sản phẩm!"));
    die;
}
if ($product_color == "") {
    echo json_encode(array('type' => 1, 'msg' => "Vui lòng nhập màu sản phẩm!"));
    die;
}
if ($folder_temp == "" || $file_main_photo_temp == "") {
    echo json_encode(array('type' => 1, 'msg' => "Vui lòng chọn ảnh đại diện!"));
    die;
}
$extra_photo_list_name = '';

if ($folder_temp != "photo") {
    $new_path_main_photo = dirname(dirname(__FILE__)) . DS . "photo" . DS . $file_main_photo_temp;
    $new_path_main_thumb_photo = dirname(dirname(__FILE__)) . DS . "photo" . DS . "thumb_photo" . DS . "200_" . $file_main_photo_temp;
    $check_save_main_photo = rename(dirname(dirname(__FILE__)) . DS . "tmp_photo" . DS . $folder_temp . DS . $file_main_photo_temp, $new_path_main_photo);
    $check_save_main_thumb_photo = rename(dirname(dirname(__FILE__)) . DS . "tmp_photo" . DS . $folder_temp . DS . "200_" . $file_main_photo_temp, $new_path_main_thumb_photo);
    if (!$check_save_main_photo || !$check_save_main_thumb_photo) {
        echo json_encode(array('type' => 1, 'msg' => "Lưu ảnh đại diện bị lỗi!"));
        die;
    } else {
        //create extra photo from main photo
        if ($from_order_detail == 1) {
            $new_path_extra_photo = dirname(dirname(__FILE__)) . DS . "photo" . DS . "extra_photo" . DS;
            $new_path_extra_photo_thumb = dirname(dirname(__FILE__)) . DS . "photo" . DS . "extra_photo" . DS . "thumb_extra_photo" . DS;
            $new_photo_name = $product_color . "_" . $product_code . "_" . $manufacture;
            $new_photo_name = $core->convert_string_friendly($new_photo_name) . ".jpg";
            copy($new_path_main_photo, $new_path_extra_photo . $new_photo_name);
            copy($new_path_main_thumb_photo, $new_path_extra_photo_thumb . $new_photo_name);
            $extra_photo_list_name = $new_photo_name. ",";
        }
    }
    if ($product_id > 0) {
        //remove old photo
        unlink(dirname(dirname(__FILE__)) . DS . "photo" . DS . $product->main_photo);
        unlink(dirname(dirname(__FILE__)) . DS . "photo" . DS . "thumb_photo" . DS . "200_" . $product->main_photo);
    }
}

//update extra photo exist
if ($product_id > 0) {
    $extra_photo = $product->extra_photo;
    if ($extra_photo != "") {
        $array_extra_photo = explode(",", $extra_photo);

        foreach ($array_extra_photo as $name) {
            if (!in_array($name, $array_extra_photo_name)) {
                unlink(dirname(dirname(__FILE__)) . DS . "photo" . DS . "extra_photo" . DS . $name);
                unlink(dirname(dirname(__FILE__)) . DS . "photo" . DS . "extra_photo" . DS . "thumb_extra_photo" . DS . $name);
            } else {
                $extra_photo_list_name.= $name . ",";
            }
        }
    }
}
if (count($extra_photo_list) > 0 && count($extra_photo_list) == count($extra_color_list)) {
    $temp_folder_extra_photo = $_SESSION["temp_folder_extra_photo"];
    $new_path_extra_photo = dirname(dirname(__FILE__)) . DS . "photo" . DS . "extra_photo" . DS;
    $new_path_extra_photo_thumb = dirname(dirname(__FILE__)) . DS . "photo" . DS . "extra_photo" . DS . "thumb_extra_photo" . DS;
    $i = 0;
    foreach ($extra_photo_list as $extra_photo_name) {
        
        $new_photo_name = $extra_color_list[$i] . "_" . $product_code . "_" . $manufacture;
        $new_photo_name = $core->convert_string_friendly($new_photo_name) . ".jpg";
        $i++;
        $extra_photo_list_name.=$new_photo_name . ",";
        rename(dirname(__FILE__) . DS . "uploadImage" . DS . "files" . DS . $temp_folder_extra_photo . DS . $extra_photo_name, $new_path_extra_photo . $new_photo_name);
        rename(dirname(__FILE__) . DS . "uploadImage" . DS . "files" . DS . $temp_folder_extra_photo . DS . "thumbnail" . DS . $extra_photo_name, $new_path_extra_photo_thumb . $new_photo_name);
    }
    //add extra color into product color list
    $product_color.= ",".implode(",", $extra_color_list);
}
if ($extra_photo_list_name != '') {
    $extra_photo_list_name = substr($extra_photo_list_name, 0, -1);
}
$message = '';
$old_product_price = 0;
if ($product_id > 0) {

    if ($product_price != $product->product_price) {
        $old_product_price = $product->product_price;
    }
    $core->db->query("UPDATE zen_product SET manufacture='$manufacture',product_name='$product_name',
            product_code='$product_code',product_category=$product_category,main_photo ='$file_main_photo_temp',extra_photo='$extra_photo_list_name',
                description='$description',link_web='$link_web',product_price=$product_price,
                    old_product_price=$old_product_price,store_price=$store_price,
                     stamp_price=$stamp_price, product_size='$product_size',product_color='$product_color'  
             WHERE product_id=$product_id");
    $message = "Update thông tin sản phẩm thành công!";
} else {

    $core->db->query("INSERT INTO zen_product (`manufacture`,`product_name`,`product_code`,product_category,`main_photo`,
        `extra_photo`,`description`,`link_web`,`product_price`,`old_product_price`,
        `store_price`,`stamp_price`,`product_size`,`product_color`)
                        VALUES('$manufacture','$product_name','$product_code',$product_category,'$file_main_photo_temp',
                            '$extra_photo_list_name','$description', '$link_web',$product_price,$old_product_price,
                        $store_price,$stamp_price,'$product_size','$product_color')");
    $product_id = $core->db->insert_id;
    if ($product_id > 0) {
        $message = "Thêm sản phẩm thành công!";
    } else {
        echo json_encode(array('type' => 1, 'msg' => "Thêm sản phẩm thất bại!"));
        die;
    }
}

echo json_encode(array('type' => 0, 'msg' => $message, 'product_id' => $product_id));
die;
?>